home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap04 / FloatingAgent.java < prev    next >
Text File  |  1996-09-19  |  1KB  |  46 lines

  1. //
  2. // an agent is floating randomly.
  3. //
  4.  
  5. import java.util.*;
  6. import vrml.*;
  7. import vrml.node.*;
  8. import vrml.field.*;
  9.  
  10. public class FloatingAgent extends Script{
  11.     SFVec3f setAgentPosition;
  12.     float agentPosition[] = new float[3];
  13.     Random randomNumGenerator = new Random();
  14.     
  15.     public void initialize(){
  16.         // get the reference of the event-out 'setAgentPosition'.
  17.         setAgentPosition = (SFVec3f)getEventOut("setAgentPosition");
  18.         
  19.         // initialize the agent position.
  20.         agentPosition[0] = 0.0f;
  21.         agentPosition[1] = 0.0f;
  22.         agentPosition[2] = 0.0f;
  23.     }
  24.     
  25.     public void processEvent(Event e){
  26.         if(e.getName().equals("interval") == true){
  27.             moveAgent();
  28.         }
  29.     }
  30.     
  31.     // generate random float value ranging between -0.1 to 0.1.
  32.     float generateRandomFloat(){
  33.         return(randomNumGenerator.nextFloat() * 0.2f - 0.1f);
  34.     }
  35.  
  36.     // move the agent randomly.
  37.     void moveAgent(){
  38.         agentPosition[0] += generateRandomFloat();
  39.         agentPosition[1] += generateRandomFloat();
  40.         agentPosition[2] += generateRandomFloat();
  41.  
  42.         // move the agent to the new position.
  43.         setAgentPosition.setValue(agentPosition);
  44.     }
  45. }
  46.